import {Component,OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {AuthService} from "../service/auth.service";
import {ToastyService, ToastOptions, ToastData} from 'ng2-toasty';

@Component({
    moduleId: module.id,
    selector: 'my-login',
    templateUrl: 'login.component.html',
    styleUrls: ['login.component.css'],
})

export class LoginComponent implements OnInit{

  phone:string;
  password:string;
  remember:any;

  constructor(public authService: AuthService, public router: Router, private toastyService:ToastyService) {
  };


  ngOnInit(): void {

  }

  login() {
    this.authService.login(this.phone, this.password).subscribe(() => {
      if (this.authService.isLoggedIn) {
        if (this.remember === true) {
          localStorage.setItem('remember', this.authService.getCompactJws());
        }
        const toastOptions:ToastOptions = {
          title: "提示信息",
          msg: "登录成功！",
          showClose: true,
          timeout: 1200,
          theme: 'bootstrap',
          onAdd: (toast:ToastData) => {},
          onRemove: (toast:ToastData) => {
            let redirect = this.authService.redirectUrl ? this.authService.redirectUrl : '/index';
            this.router.navigate([redirect]);
          }
        };
        this.toastyService.success(toastOptions);
      }
    });
  }

}
